tests: implement pytest_test rule for internal test usage#3931
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a new pytest_test rule to support running pytest tests under Bazel, along with corresponding dependency updates and integration tests. Feedback on the changes highlights several critical issues: calling pytest_bazel.main in the bootstrap template will cause an AttributeError as pytest.main should be called instead; the config_settings attribute is unsupported in pytest_test and will cause a loading-phase error; the "pypi" repository is not exported by dev_pip in MODULE.bazel; list concatenation with srcs in pytest_test.bzl will fail if srcs is a select(); template expansion can be simplified by avoiding computed_substitutions; and the bzl_library target for pytest_test is missing its srcs attribute.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| py_test( | ||
| name = name, | ||
| main = main_file, | ||
| srcs = [bootstrap_target] + srcs, |
There was a problem hiding this comment.
If srcs is a select(), concatenating it as [bootstrap_target] + srcs will fail with a Starlark evaluation error because list-plus-select concatenation is not supported (only select-plus-list is supported). Reversing the order to srcs + [bootstrap_target] resolves this and makes the rule compatible with select()s.
| srcs = [bootstrap_target] + srcs, | |
| srcs = srcs + [bootstrap_target], |
There was a problem hiding this comment.
The bootstrap is not needed, we can just use the main_module that already exists in the py_test rule.
aignas
left a comment
There was a problem hiding this comment.
I need a little bit of time to think how:
- We can handle sharding related optional dependencies.
- We can handle other features that may require extra plugins.
Since pytest_bazel is its own repo, either we have to migrate the code to rules_python or migrate the rule imple to pytest_bazel and then proxy it in rules_python. Let me think about it a little.
Other ideas/thoughts here are welcome.
| py_test( | ||
| name = name, | ||
| main = main_file, | ||
| srcs = [bootstrap_target] + srcs, |
There was a problem hiding this comment.
The bootstrap is not needed, we can just use the main_module that already exists in the py_test rule.
How so? I originally tried that, but it didn't work. I didn't see how it could work in practice -- it would have to somehow auto discover the subset of files to test, but it has no info about what that subset is. |
Implements a new rule to support running Python tests with pytest. Exposes default pytest targets and includes basic tests.
b925c46 to
0dba5a1
Compare
That's OK with me, but: How about I move it to tests/support for now? I'd like to port the tests for e.g. the release tool to pytest, but I'm somewhat blocked on having a functional rule for it. This would also help give us some ideas of what basic usage looks like. |
|
+1 on having it in support for some time. |
It will discover all of the tests that are available in a particular sandbox. I remember those working at some point. And it should ignore any tests in the external dependencies. |
|
Tested the |
|
Moved to tests/support
I added these as open questions to the issue description. |
|
FYI, a bzlmod-only example is here: aignas/pytest-bazel#30, feel free to leave any comments. |
|
|
||
| import pytest_bazel | ||
|
|
||
| TEST_FILES = """%TEST_FILES%""".splitlines() |
There was a problem hiding this comment.
I still don't understand why this is necessary, because pytest will discover the test files in the current directory traversing it down, but I am happy to discuss this later to unblock the usage of pytest-bazel.
|
Ah, i think i see now: because it goes through test collection, it has to follow whatever rules pytest has identifying test files. If you name a test just "foo.py", then it won't see it. |
This implements a pytest_test rule based upon the pytest_bazel library.
The core implementation of this is fairly simple: a file is generated that calls
pytest.main()with thesrcsto test. This generated file is the file that isrun by
py_test.For now, this is kept under tests/support soas to use it for our own tests
while some design decisions are figured out.
Work towards #3594